package com.xfly.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.view.TitleBarView;
import com.xfly.app.adapter.LbAdapter;
import com.xfly.app.bean.LbBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LbActivity extends BaseMvpActivity {

    TitleBarView title_bar;

    ListView listview;

    LbAdapter lbAdapter;

    List<LbBean> lbBeans = new ArrayList<>();

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();

    TextView add;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_lb);
    }

    @Override
    public void initViews() {
        title_bar = findViewById(R.id.title_bar);
        listview = findViewById(R.id.listview);
        add = findViewById(R.id.add);

        title_bar.setText("详情");
    }

    @Override
    public void initEvents() {
        title_bar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(),AddLbActivity.class));
            }
        });


        lbAdapter = new LbAdapter(getContext(),lbBeans);
        listview.setAdapter(lbAdapter);

        lbAdapter.setClick(new LbAdapter.Click() {
            @Override
            public void Click(int position) {
                Intent intent = new Intent(getContext(),AddLbActivity.class);
                intent.putExtra("bean",lbBeans.get(position));
                startActivity(intent);
            }

            @Override
            public void Click2(int position) {
                dellb(lbBeans.get(position).getId()+"");
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        getlist();
    }

    private void getlist(){
        Map<String,String> map = new HashMap<>();
        map.put("page","1");
        map.put("perpage","100");
        apiDataRepository.lblist(map, new ApiNetResponse<List<LbBean>>() {
            @Override
            public void onSuccess(List<LbBean> data) {
                lbBeans = data;
                lbAdapter.pushData(data);
            }
        });
    }

    private void dellb(String id){
        Map<String,String> map = new HashMap<>();
        map.put("slides_id",id);
        apiDataRepository.dellb(map, new ApiNetResponse<Object>() {
            @Override
            public void onSuccess(Object data) {
                getlist();
            }
        });
    }
}
